/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       flotsam.h

	$Header: /game/flotsam.h $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "flotsam.h"

#include "adv_object_type.h"
#include "adventure_frame.h"
#include "adventure_sounds.h"
#include "army.h"
#include "basic_dialog.h"
#include "format_string.h"
#include "material_names.h"
#include "music.h"
#include "object_registration.h"
#include "player.h"
#include "random.h"
#include "replace_keywords.h"

static t_object_registration<t_flotsam> k_registration( k_adv_object_flotsam );

// -----------------------------------------------------------------
// flotsam.  Contains wood and sometimes gold.
// -----------------------------------------------------------------
t_flotsam::t_flotsam( t_stationary_adventure_object const& source ) 
         : t_stationary_adventure_object( source )
{
	m_wood = random( 5, 10 );
	m_gold = 1000 - (m_wood * 100);
}

// -----------------------------------------------------------------
// flotsam.  Contains wood and sometimes gold.
// -----------------------------------------------------------------
void t_flotsam::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                          t_direction direction, t_adventure_frame* frame )
{
	t_player* owner = army->get_owner();

	owner->gain( k_gold, m_gold );
	owner->gain( k_wood, m_wood );
	if (!army->get_owner()->is_computer())
	{
		t_screen_point           screen_point;
		t_basic_dialog*          dialog;
		std::string              text;
		t_material_array         rewards;

		// initialize dialog
		dialog = new t_basic_dialog;
		text = get_text( "initial" );
		rewards[k_wood] = m_wood;
		rewards[k_gold] = m_gold;
		text = replace_keywords( text, rewards );
		dialog->add_materials( rewards );
		dialog->set_text( text );
		dialog->add_ok_button();
		frame->update_funds();
		dialog->set_title( get_name() );
		dialog->open( get_dialog_bottom( point ), true, k_align_bottom );
		t_sound_cache music = get_music_playing();
		stop_music();
		get_dialog_sound( k_dialog_sound_treasure )->play( get_sound_volume() );
		dialog->run_modal();
		play_music( music );
	}
	destroy();
}

// -----------------------------------------------------------------
// flotsam.  Contains wood and sometimes gold.
// -----------------------------------------------------------------
bool t_flotsam::read( std::streambuf&						stream, 
					  t_qualified_adv_object_type const&	type,
					  int									version )
{
	m_gold = get<int>( stream );
	m_wood = get<int>( stream );
	return true;
}

// -----------------------------------------------------------------
// flotsam.  Contains wood and sometimes gold.
// -----------------------------------------------------------------
bool t_flotsam::write( std::streambuf& stream ) const
{
	put<int>( stream, m_gold );
	put<int>( stream, m_wood );
	return true;
}

float t_flotsam::ai_value( t_adventure_ai const& ai, t_creature_array const& army, int move_cost ) const
{
	t_player* player = army.get_owner();
	t_material_values const& values = player->get_material_values();
	return values[k_gold] * m_gold + values[k_wood] * m_wood;
}
